//
//  YKFarmWaterViewController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/12.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmWaterViewController.h"
#import "YKFarmViewModel.h"
#import <YK_TableRefresh/YK_TableRefresh.h>
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import "YKFarmMotifyController.h"

@interface YKFarmWaterViewController ()<UITableViewDelegate,UITableViewDataSource>
///
@property(nonatomic,strong) YKFarmViewModel *viewModel;
///
@property(nonatomic,copy) NSString *farmId;
///
@property(nonatomic,strong) UITableView *tableView;

@end

@implementation YKFarmWaterViewController
- (instancetype)initWithViewModel:(YKFarmViewModel *)viewModel farmId:(NSString *)farmId
{
    self = [super init];
    if (self) {
        self.farmId = farmId;
        self.viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.viewModel.farmWaterCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    [[self.viewModel.farmWaterCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在加载"];
        }else
        {
            [YKAlertCenter dissLoading];
        }
    }];
    [self.viewModel.saveWaterComm.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"保存成功"];
        [self.tableView reloadData];
    }];
    [self.viewModel.farmWaterCommand execute:self.farmId];
    // Do any additional setup after loading the view.
}

#pragma mark -tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.farmWaterDic.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass(UITableViewCell.class)];
        cell.backgroundColor = UIColor.clearColor;
        cell.contentView.backgroundColor = UIColor.clearColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    @weakify(cell);
    [self.viewModel getFarmWater:indexPath block:^(NSString * _Nonnull name, NSString * _Nonnull detail) {
        @strongify(cell);
        cell.textLabel.text = [NSString stringWithFormat:@"%@      :     %@",name,detail];
    }];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    [self.viewModel getFarmWater:indexPath block:^(NSString * _Nonnull name, NSString * _Nonnull detail) {
        @strongify(self);
        YKFarmMotifyController *vc = [[YKFarmMotifyController alloc] initWithdetail:detail block:^(NSString * _Nonnull detail) {
            @strongify(self);
            [self.viewModel setWaterDetil:indexPath detail:detail];
            [self.viewModel.saveWaterComm execute:nil];
        }];
        vc.title = name;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 85;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.backgroundColor = UIColor.clearColor;
        @weakify(self);
        tableView.mj_header = [YKRefreshHeader headerNormalWithViewModel:self.viewModel dataSource:self.viewModel.farmListArray refreshingBlock:^{
            @strongify(self);
            //刷新动作
            [self.viewModel.farmWaterCommand execute:self.farmId];
        }];
        tableView.mj_footer = [YKRefreshFooter footerWithViewModel:self.viewModel refreshingBlock:^{
            @strongify(self);
            [self.viewModel.farmWaterCommand execute:self.farmId];
        }];
        _tableView = tableView;
    }
    return _tableView;
}

- (void)dealloc
{
    self.viewModel.farmWaterDic = [NSMutableDictionary dictionary];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
